package org.easyupdater;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.easyupdater.model.ApplicationSpec;
import org.easyupdater.model.BundleSpec;

/**
 * TODO Make sure we don't remove one of own own bundles 
 * 
 * @author Bogdan Dimitriu
 */
public class BundleUpgradeManager implements IBundleUpgradeManager {

	/**
	 * @see org.easyupdater.IBundleUpgradeManager#getBundlesToRemove(org.easyupdater.model.ApplicationSpec, org.easyupdater.model.ApplicationSpec)
	 */
	public Set<BundleSpec> getBundlesToRemove(
			ApplicationSpec oldApplicationSpec,
			ApplicationSpec newApplicationSpec) {
		if (oldApplicationSpec == null)
			return Collections.emptySet();
		Set<BundleSpec> bundlesToRemove = new HashSet<BundleSpec>();
		for (BundleSpec bundleSpec : oldApplicationSpec.getBundleSpecs()) {
			if (!newApplicationSpec.getBundleSpecs().contains(bundleSpec)) {
				bundlesToRemove.add(bundleSpec);
			}
		}
		return bundlesToRemove;
	}

	/**
	 * @see org.easyupdater.IBundleUpgradeManager#getBundlesToInstall(org.easyupdater.model.ApplicationSpec, org.easyupdater.model.ApplicationSpec)
	 */
	public Set<BundleSpec> getBundlesToInstall(
			ApplicationSpec oldApplicationSpec,
			ApplicationSpec newApplicationSpec) {
		Set<BundleSpec> bundlesToInstall = new HashSet<BundleSpec>();
		if (oldApplicationSpec == null) {
			bundlesToInstall.addAll(newApplicationSpec.getBundleSpecs());
		} else {
			for (BundleSpec bundleSpec : newApplicationSpec.getBundleSpecs()) {
				if (!oldApplicationSpec.getBundleSpecs().contains(bundleSpec)) {
					bundlesToInstall.add(bundleSpec);
				}
			}
		}
		return bundlesToInstall;
	}
}
